/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.OutletManagement;

import session.TX.DishManagerSBRemote;
import managedbean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import entity.FoodItem;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "foodItemManagedBean")
@ViewScoped
public class FoodItemMB {

    @EJB
    DishManagerSBRemote dishBean;
    @ManagedProperty(value = "#{TrackerOutletAdmin}")
    private TrackerManagedOutletMB trackerManagedOutletMB;
     @ManagedProperty(value = "#{photoManagedBean}")
    private PhotoMB photoMB;
    Long id;
    String name;
    String description;
    Double cost;
    String itemType;
    List<FoodItem> itemList = null;
    String statusMsgStr = "";

    /** Creates a new instance of AlaCarteMgmtBean */
    public FoodItemMB() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getCost() {
        return cost;
    }

    public void setCost(Double cost) {
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getItemType() {
        return itemType;
    }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }
    
    public String getStatusMsgStr() {
        return statusMsgStr;
    }

    public void setStatusMsgStr(String statusMsgStr) {
        this.statusMsgStr = statusMsgStr;
    }



    public void addNewFoodItem(ActionEvent event) {
        itemList = null;
        dishBean.addFoodItem(trackerManagedOutletMB.getOutletId(), name, description, cost, itemType,getPhotoMB().getDishFilePath() );
    }
    
    public static Boolean strToboolean(String s) {
        if (s.equalsIgnoreCase("true")) {
            return true;
        } else {
            return false;
        }
    }

    public void modifyFoodItem(ActionEvent event) {
        itemList = null;
        dishBean.editFoodItem(id, name, description, cost);
    }
    
    public List<FoodItem> getFoodItem (){
        if (this.itemList == null){
            itemList = this.getFoodItemFrmDatabase();
        }
        return itemList;
    }
    
    private List<FoodItem> getFoodItemFrmDatabase(){
        return dishBean.getOutletFoodMenu(trackerManagedOutletMB.getOutletId());
    }

    public void deleteFoodItem(ActionEvent event) {
        itemList = null;
        Long id = (Long) event.getComponent().getAttributes().get("itemId");
        dishBean.deleteFoodItem(id, trackerManagedOutletMB.getOutletId());
    }
    
    public void onRowEdit(RowEditEvent event){
        FoodItem item = (FoodItem) event.getObject();
        System.out.println(item);
        dishBean.editFoodItem(item);
    }

    public String getOutletName() {
        return trackerManagedOutletMB.getOutletName();
    }
    
    public List<String> getAvailableFoodTypes(){
        List returnList = new ArrayList<String>();
        returnList.add("Menu Item");
        returnList.add("Featured Dish");
        returnList.add("Promotional Item");
        returnList.add("Festive Item");
        returnList.add("Reward Meals");
        return returnList;
    }

    /**
     * @return the photoMB
     */
    public PhotoMB getPhotoMB() {
        return photoMB;
    }

    /**
     * @param photoMB the photoMB to set
     */
    public void setPhotoMB(PhotoMB photoMB) {
        this.photoMB = photoMB;
    }

    /**
     * @return the trackerManagedOutletMB
     */
    public TrackerManagedOutletMB getTrackerManagedOutletMB() {
        return trackerManagedOutletMB;
    }

    /**
     * @param trackerManagedOutletMB the trackerManagedOutletMB to set
     */
    public void setTrackerManagedOutletMB(TrackerManagedOutletMB trackerManagedOutletMB) {
        this.trackerManagedOutletMB = trackerManagedOutletMB;
    }
}
